package com.huaxia.day11;

import java.util.*;
import java.util.stream.Collectors;

public class ArrayListTest {
    public static void main(String[] args) {
        ArrayListTest test = new ArrayListTest();
        List<Student> students = test.create();
        printList(students);

        //age 排序
//        test.sortByAge(students);
//        sortBy8(students);
        list2Array(students);
    }


    /**
     * 1. 类图结构查看
     * 2.初始化
     * 3.
     */

    public static List<Student> create() {
        List<Student> list = new ArrayList<>(5);
        for (int i = 0; i < 10; i++) {
            Student st = new Student("name_" + i, (int) (Math.random() * 80));
            list.add(st);
        }
        return list;
    }

    public static void printList(List<Student> list) {
        for (Student s : list) {
            //  System.out.println(s.toString());
        }
        System.out.println(list.size());
        Iterator it = list.iterator();// 迭代器方式
        while (it.hasNext()) {
            System.out.println(it.next().toString());
        }
    }

    /**
     * Collections 排序
     */
    public static void sortByAge(List<Student> list) {
        Collections.sort(list, new StudentComparator());
        for (Student s : list) {
            System.out.println(s.toString());
        }
    }

    /**
     * stream 排序
     *
     * @param list
     */
    public static void sortBy8(List<Student> list) {
        List<Student> res = list.stream().sorted((o1, o2) -> o1.getAge() - o2.getAge()).collect(Collectors.toList());
        printList(res);
    }

    /**
     *
     * @param list
     * @return list ,数组互转
     */
    public static Student[] list2Array(List<Student> list){
        Student [] ss =list.toArray(new Student[0]);
         System.out.println("array size:"+ss.length);
         List<Student> ll = Arrays.asList(ss);
         printList(ll);
        return ss;
    }
}
